home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / SYS / VMS / FPARSE.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  5KB  |  179 lines

  1. /*
  2.  *From ut-ngp!ut-sally!im4u!rutgers!nike!ucbcad!ucbvax!YALE.ARPA!LEICHTER-JERRY
  3.  * Thu Nov  6 08:29:57 CST 1986
  4.  *
  5.  *
  6.  *         I am writing a user interface program for radio telescopes and
  7.  *    am trying to locate a piece of code to parse VMS file names.  I
  8.  *    would prefer C but we also have Fortran and Pascal compilers.  I'm
  9.  *    trying to avoid having figuring out the call to SYS$PARSE and would
  10.  *    rather not have to open the file at this point in the program.
  11.  * Try the following (I didn't write it but I do use it).
  12.  *                            -- Jerry
  13.  */
  14.  
  15. /*)LIBRARY
  16. */
  17.  
  18. /*
  19.  
  20. fparse.c - fparse routine to emulate f$parse in DCL command files
  21.  
  22. Synposis:
  23.  
  24.     char *fparse( file_name, default_name, related_name, field)
  25.         char *file_name, default_name, related_name, field;
  26.  
  27. Description:
  28.  
  29.     All strings are null-terminated.  Only the first one or two characters
  30.     are checked in field names. Parameters can be omitted by passing NULL.
  31.  
  32.     Fields are:
  33.  
  34.         "node"        node name
  35.         "device"        device name
  36.         "directory"        directory name
  37.         "name"        file name
  38.         "type"        file type
  39.         "version"        file version number
  40.  
  41.     If the field parameter is null, all fields are expanded, except that
  42.     the node name is included only if it appears in the file_name,
  43.     default_name or related_name.  Note that field parameters
  44.     must be given in lower-case.
  45.  
  46.     Within each field, the expanded name is taken from the file_name,
  47.     default_name and related_name, in that order.
  48.  
  49.     The value returned is the address of the null-terminated file name;
  50.     fparse calls malloc to reserve space for the string.
  51.  
  52.     The string "" is returned on either an RMS parse error, or
  53.     an erroroneous field parameter name.  The RMS status error
  54.     is not available.  Only one field may be given.
  55.  
  56. Example:
  57.  
  58.  
  59.     To parse a command line LINK/EXE=exefile objfile, the default extension
  60.     for "objfile" is .OBJ.  The default file name for "exefile" is the file
  61.     name of "objfile", and the default extension is .EXE.
  62.  
  63.     Say that the char * variable "objfile" points to the object file
  64.     name from the command line, and the char * "exefile" points to the
  65.     exe file from the command line.  Then to expand these into file
  66.     names for calls to open() or fopen():
  67.  
  68.         objfile = fparse( objfile, ".OBJ", NULL, NULL);
  69.         exefile = fparse( exefile, ".EXE", objfile, NULL);
  70.  
  71.     To find only the directory of the object file:
  72.  
  73.         dir = fparse( objfile, ".OBJ", NULL, "directory");
  74.  
  75.     The field name could also be abbreviated:
  76.  
  77.         dir = fparse( objfile, ".OBJ", NULL, "di");
  78.  
  79. */
  80.  
  81. #include <stdio.h>
  82. #include <rms.h>
  83. #include <ssdef.h>
  84.  
  85. char *fparse( file_name, default_name, related_name, field)
  86.     char *file_name, *default_name, *related_name, *field;
  87. {
  88.     struct FAB ff_file_fab;
  89.     struct NAM fn_file_nam, rn_related_nam, *nam;
  90.     char *expanded_name, *eptr;
  91.     int expanded_length, rms_status;
  92.     char expand_buffer[ NAM$C_MAXRSS];
  93.  
  94. /* initialize all the blocks for RMS */
  95.  
  96.     ff_file_fab = cc$rms_fab;
  97.     ff_file_fab.fab$l_nam = &fn_file_nam;
  98.     ff_file_fab.fab$l_fna = file_name;
  99.     ff_file_fab.fab$b_fns = (file_name == NULL) ? 0 : strlen( file_name);
  100.     ff_file_fab.fab$l_dna = default_name;
  101.     ff_file_fab.fab$b_dns = (default_name == NULL) ? 0 : strlen( default_name);
  102.  
  103.     fn_file_nam = cc$rms_nam;
  104.     nam = &fn_file_nam;
  105.     nam->nam$l_esa = expand_buffer;
  106.     nam->nam$b_ess = NAM$C_MAXRSS;
  107.     nam->nam$l_rlf = &rn_related_nam;
  108.  
  109.     rn_related_nam = cc$rms_nam;
  110.     rn_related_nam.nam$l_rsa = related_name;
  111.     rn_related_nam.nam$b_rsl =
  112.       (related_name == NULL) ? 0 : strlen( related_name);
  113.  
  114. /* call SYS$PARSE to parse the file name */
  115.  
  116.     rms_status = sys$parse( &ff_file_fab);
  117.     if (rms_status != RMS$_NORMAL)
  118.     {  /* error in parse, so return empty string */
  119.     expanded_name = malloc(1);
  120.     expanded_name[0] = '\0';
  121.     return( expanded_name);
  122.     }
  123.  
  124. /* construct the expanded file name */
  125.  
  126.     if (field == NULL || field[0] == '\0')
  127.     {  /* caller wants all fields */
  128.     expanded_length = nam->nam$b_esl;
  129.     eptr = expand_buffer;
  130.     }
  131.     else
  132.     {  /* caller wants just one field */
  133.     switch (field[0])
  134.         {
  135.         case 'n':        /* node or name */
  136.         if (field[1] == 'o')
  137.             {        /* node */
  138.             expanded_length = nam->nam$b_node;
  139.             eptr = nam->nam$l_node;
  140.             }
  141.         else
  142.             {        /* name */
  143.             expanded_length = nam->nam$b_name;
  144.             eptr = nam->nam$l_name;
  145.             }
  146.         break;
  147.         case 'd':        /* device or directory */
  148.         if (field[1] == 'e')
  149.             {        /* device */
  150.             expanded_length = nam->nam$b_dev;
  151.             eptr = nam->nam$l_dev;
  152.             }
  153.         else
  154.             {        /* directory */
  155.             expanded_length = nam->nam$b_dir;
  156.             eptr = nam->nam$l_dir;
  157.             }
  158.         break;
  159.         case 't':        /* type */
  160.         expanded_length = nam->nam$b_type;
  161.         eptr = nam->nam$l_type;
  162.         break;
  163.         case 'v':        /* version */
  164.         expanded_length = nam->nam$b_ver;
  165.         eptr = nam->nam$l_ver;
  166.         break;
  167.         default:
  168.         expanded_length = 0;
  169.         break;
  170.         }
  171.     }
  172.     expanded_name = malloc( expanded_length + 1);
  173.     strncpy( expanded_name, eptr, expanded_length);
  174.     expanded_name[ expanded_length] = '\0';
  175.     return( expanded_name);
  176. }
  177.  
  178.  
  179.